package com.shinewind.info.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.home.service.IModelService;
import com.shinewind.home.service.INewsService;
import com.shinewind.info.mapper.IArticleMapper;
import com.shinewind.info.service.IArticleService;
import com.shinewind.pojo.entity.Article;
import com.shinewind.pojo.entity.Model;
import com.shinewind.pojo.entity.News;
import com.shinewind.pojo.vo.ArticleVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl extends ServiceImpl<IArticleMapper, Article> implements IArticleService {
    @DubboReference
    private INewsService iNewsService;
    @DubboReference
    private IModelService iModelService;

    @Override
    public Article getArticleById(String mId) {
        Article article = this.lambdaQuery().eq(Article::getMId,mId).one();
        return article;
    }

    @Override
    public ArticleVo getArticleByMid(String mId) {
        //查询文章相关信息
        Article article = this.getArticleById(mId);
        ArticleVo articleVo = ArticleVo.builder().build();
        BeanUtils.copyProperties(article,articleVo);
        //查询相关商品或新闻
        Optional<News> news = Optional.ofNullable(iNewsService.getNewsInfoById(article.getMId()));
        if(ObjectUtil.isNotNull(news) && "1".equals(news.get().getIsModel())){
            Model model = iModelService.getModelByMId(mId);
            articleVo.setName(model.getTitle());
            articleVo.setSrc(model.getHref());
            articleVo.setIsModel(news.get().getIsModel());
        }
        else if(ObjectUtil.isNotNull(news) && "0".equals(news.get().getIsModel())){
            articleVo.setName(news.get().getTitle());
            articleVo.setSrc(news.get().getHref());
            articleVo.setIsModel(news.get().getIsModel());
        }
        return articleVo;
    }

    @Override
    public List<ArticleVo> getAllArticleVo() {
        //查询文章相关信息
        List<Article> articles = this.lambdaQuery().list();
        List<ArticleVo> articleVos = articles.stream().map(article -> {
            ArticleVo articleVo = ArticleVo.builder().build();
            BeanUtils.copyProperties(article,articleVo);
            //查询相关商品或新闻
            Optional<News> news = Optional.ofNullable(iNewsService.getNewsInfoById(article.getMId()));
            if(ObjectUtil.isNotNull(news) && "1".equals(news.get().getIsModel())){
                Model model = iModelService.getModelByMId(article.getMId());
                articleVo.setName(model.getTitle());
                articleVo.setSrc(model.getHref());
                articleVo.setIsModel(news.get().getIsModel());
            }
            else if(ObjectUtil.isNotNull(news) && "0".equals(news.get().getIsModel())){
                articleVo.setName(news.get().getTitle());
                articleVo.setSrc(news.get().getHref());
                articleVo.setIsModel(news.get().getIsModel());
            }
            return articleVo;
        }).collect(Collectors.toList());
        return articleVos;
    }

    @Override
    public Article getArticleByAid(Integer aId) {
        Article article = this.lambdaQuery().eq(Article::getAId,aId).one();
        return article;
    }

    @Override
    public Long getArticleCountByUid(String uId) {
        Long count = this.lambdaQuery().eq(Article::getUId,uId).count();
        return count;
    }
}
